]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
history: Fix HistoryManager
authorJasper St. Pierre <jstpierre@mecheye.net>
Mon, 28 Feb 2011 17:22:12 +0000 (12:22 -0500)
committerJasper St. Pierre <jstpierre@mecheye.net>
Fri, 4 Mar 2011 14:07:15 +0000 (09:07 -0500)
Make GSettings support optional, refactor text entry handling,
fix some off-by-one bugs in the management itself, use Params
for parsing, fix other typos and bugs.

https://bugzilla.gnome.org/show_bug.cgi?id=642793

js/misc/history.js
js/ui/lookingGlass.js
js/ui/runDialog.js

index 69f27f9dd5b8a9530670fe7fd2491c693eddb83a..24d1b514d3a2fb153e2566b1041c61821d5dc844 100644 (file)
@@ -2,22 +2,40 @@
 
 const Lang = imports.lang;
 const Signals = imports.signals;
+const Clutter = imports.gi.Clutter;
+const Params = imports.misc.params;
 
 const DEFAULT_LIMIT = 512;
 
-function HistoryManager(settings_key) {
-    this._init(settings_key);
+function HistoryManager(params) {
+    this._init(params);
 }
 
 HistoryManager.prototype = {
-    _init: function(settings_key, limit) {
-        this._limit = limit || DEFAULT_LIMIT;
-        this._key = settings_key;
-        this._history = global.settings.get_strv(settings_key);
-        this._historyIndex = -1;
-
-        global.settings.connect('changed::' + settings_key,
-                                Lang.bind(this, this._historyChanged));
+    _init: function(params) {
+        params = Params.parse(params, { gsettingsKey: null,
+                                        limit: DEFAULT_LIMIT,
+                                        entry: null });
+
+        this._key = params.gsettingsKey;
+        this._limit = params.limit;
+
+        this._historyIndex = 0;
+        if (this._key) {
+            this._history = global.settings.get_strv(this._key);
+            global.settings.connect('changed::' + this._key,
+                                    Lang.bind(this, this._historyChanged));
+
+        } else {
+            this._history = [];
+        }
+
+        this._entry = params.entry;
+
+        if (this._entry) {
+            this._entry.connect('key-press-event', 
+                                Lang.bind(this, this._onEntryKeyPress));
+        }
     },
 
     _historyChanged: function() {
@@ -26,18 +44,32 @@ HistoryManager.prototype = {
     },
 
     prevItem: function(text) {
-        this._setHistory(this._historyIndex--, text);
+        if (this._historyIndex <= 0)
+            return text;
+
+        if (text)
+            this._history[this._historyIndex] = text;
+        this._historyIndex--;
         return this._indexChanged();
     },
 
     nextItem: function(text) {
-        this._setHistory(this._historyIndex++, text);
+        if (this._historyIndex >= this._history.length)
+            return text;
+
+        if (text)
+            this._history[this._historyIndex] = text;
+        this._historyIndex++;
         return this._indexChanged();
     },
 
     lastItem: function() {
-        this._historyIndex = this._history.length;
-        return this._indexChanged();
+        if (this._historyIndex != this._history.length) {
+            this._historyIndex = this._history.length;
+            this._indexChanged();
+        }
+
+        return this._historyIndex[this._history.length];
     },
 
     addItem: function(input) {
@@ -45,28 +77,39 @@ HistoryManager.prototype = {
             this._history[this._history.length - 1] != input) {
 
             this._history.push(input);
+            this._historyIndex = this._history.length;
             this._save();
         }   
     },
 
+    _onEntryKeyPress: function(entry, event) {
+        let symbol = event.get_key_symbol();
+        if (symbol == Clutter.KEY_Up) {
+            this.prevItem(entry.get_text());
+            return true;
+        } else if (symbol == Clutter.KEY_Down) {
+            this.nextItem(entry.get_text());
+            return true;
+        }
+        return false;
+    },
+
     _indexChanged: function() {
         let current = this._history[this._historyIndex] || '';
         this.emit('changed', current);
-        return current;
-    },
 
-    _setHistory: function(index, text) {
-        this._historyIndex = Math.max(this._historyIndex, 0);
-        this._historyIndex = Math.min(this._historyIndex, this._history.length);
+        if (this._entry)
+            this._entry.set_text(current);
 
-        if (text)
-            this._history[index] = text;
+        return current;
     },
 
     _save: function() {
         if (this._history.length > this._limit)
-            this._history.splice(0, this._history.length - this._key);
-        global.settings.set_strv(this._key, this._history);
+            this._history.splice(0, this._history.length - this._limit);
+
+        if (this._key)
+            global.settings.set_strv(this._key, this._history);
     }
 };
 Signals.addSignalMethods(HistoryManager.prototype);
index 8caf657d93f306fa86d3b95a7f6e4bb5e53a3c54..ede95a16181e233416a819ab992c596bcd97beee 100644 (file)
@@ -778,23 +778,8 @@ LookingGlass.prototype = {
             return true;
         }));
 
-        this._history = new History.HistoryManager(HISTORY_KEY);
-        this._history.connect('changed', Lang.bind(this, function(history, text) {
-            this._entry.text = text;
-        }));
-
-        this._entry.clutter_text.connect('key-press-event', Lang.bind(this, function(o, e) {
-            let symbol = e.get_key_symbol();
-            if (symbol == Clutter.Up) {
-                this._history.prevItem(o.get_text());
-                return true;
-            } else if (symbol == Clutter.Down) {
-                this._history.nextItem(o.get_text());
-                return true;
-            } else {
-                return false;
-            }
-        }));
+        this._history = new History.HistoryManager({ gsettingsKey: HISTORY_KEY, 
+                                                     entry: this._entry.clutter_text });
     },
 
     _updateFont: function() {
index 219191d46666d7f1538cb14048850520224c643f..15340d359a7a0f369e27136664f15021bf2d622e 100644 (file)
@@ -234,21 +234,10 @@ __proto__: ModalDialog.ModalDialog.prototype,
         this._commandCompleter = new CommandCompleter();
         this._group.connect('notify::visible', Lang.bind(this._commandCompleter, this._commandCompleter.update));
 
-        this._history = new History.HistoryManager(HISTORY_KEY);
-        this._history.connect('changed', Lang.bind(this, function(history, text) {
-            this._entryText.set_text(text);
-        }));
-
+        this._history = new History.HistoryManager({ gsettingsKey: HISTORY_KEY,
+                                                     entry: this._entryText });
         this._entryText.connect('key-press-event', Lang.bind(this, function(o, e) {
             let symbol = e.get_key_symbol();
-            if (symbol == Clutter.Down) {
-                this._history.nextItem(o.get_text());
-                return true;
-            }
-            if (symbol == Clutter.Up) {
-                this._history.prevItem(o.get_text());
-                return true;
-            }
             if (symbol == Clutter.Return || symbol == Clutter.KP_Enter) {
                 if (Shell.get_event_state(e) & Clutter.ModifierType.CONTROL_MASK)
                     this._run(o.get_text(), true);